<?php
namespace Admin\Controller;
/**
 *自定义级联数据操作类
 * @author ran
 */
class CasecadeController extends AdminController {

    public function index(){
        $pid  = I('get.pid',0);
        if($pid){
            $data = M('Casecade')->where("item_pid={$pid}")->field(true)->find();
            $this->assign('data',$data);
        }
        $name      =   trim(I('get.name'));
        $type       =   C('CONFIG_GROUP_LIST');
        $all_data   =   M('Casecade')->getField('item_id,item_name');
        $map['item_pid'] =   $pid;
        if($title)
            $map['item_name'] = array('like',"%{$name}%");
        $list       =   M("Casecade")->where($map)->field(true)->order('sort asc,item_id asc')->select();
        int_to_string($list,array('disabled'=>array(1=>'是',0=>'否')));
        if($list) {
            foreach($list as &$key){
                if($key['item_pid']){
                    $key['up_name'] = $all_data[$key['item_pid']];
                }
            }
            $this->assign('list',$list);
        }
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);

        $this->meta_title = '级联数据列表';
        $this->display();
    }
    
    public function add(){
    	if(IS_POST){
    		$caseM = D('Casecade');
    		$data = $caseM->create();
    		if($data){
    			$id = $caseM->add();
    			if($id){
    				//TODO 重置缓存
    				$this->success('新增成功', Cookie('__forward__'));
    			} else {
    				$this->error('新增失败');
    			}
    		} else {
    			$this->error($caseM->getError());
    		}
    	} else {
    		$this->assign('info',array('item_pid'=>I('pid')));
    		$list = M('Casecade')->field(true)->select();
    		$list = D('Common/Tree')->toFormatTree($list);
    		$list = array_merge(array(0=>array('id'=>0,'title_show'=>'选项分组名称')), $list);
    		$this->assign('list', $list);
    		$this->meta_title = '新增数据项';
    		$this->display('edit');
    	}
    }
    
    
    public function disable(){
    	$id = array_unique((array)I('id',0));
    
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    
    	$map = array('item_id' => array('in', $id) );
    	if(M('Casecade')->where($map)->setField('disabled',1)){
    		//TODO 清空缓存
    		$this->success('禁用成功');
    	} else {
    		$this->error('禁用失败！');
    	}
    }
    
    public function sort(){
    	if(IS_GET){
    		$ids = I('get.ids');
    		$pid = I('get.pid');
    
    		//获取排序的数据
    		$map = array('disabled'=>array('neq',1));
    		if(!empty($ids)){
    			$map['item_id'] = array('in',$ids);
    		}else{
    			if($pid !== ''){
    				$map['item_pid'] = $pid;
    			}
    		}
    		$list = M('Casecade')->where($map)->field('item_id,item_name')->order('sort asc,id asc')->select();
    
    		$this->assign('list', $list);
    		$this->meta_title = '数据排序';
    		$this->display();
    	}elseif (IS_POST){
    		$ids = I('post.ids');
    		$ids = explode(',', $ids);
    		foreach ($ids as $key=>$value){
    			$res = M('Casecade')->where(array('item_id'=>$value))->setField('sort', $key+1);
    		}
    		if($res !== false){
    			//TODO 清空缓存
    			$this->success('排序成功！');
    		}else{
    			$this->error('排序失败！');
    		}
    	}else{
    		$this->error('非法请求！');
    	}
    }
}